import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import * as dto from './DTO';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/sales/api/v1';

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询列表'
});

const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询详情'
});

const getAddDesc = formatMessage({
    id: 'api.getAdd',
    defaultMessage: '新增'
});

const getModifyDesc = formatMessage({
    id: 'api.getModify',
    defaultMessage: '修改'
});

const confirmDesc = formatMessage({
    id: 'api.confirm',
    defaultMessage: '确认'
});

const getAbandonDesc = formatMessage({
    id: 'api.getAbandon',
    defaultMessage: '作废'
});

const getCancelDesc = formatMessage({
    id: 'api.getCancel',
    defaultMessage: '取消Pi'
});

const getBankDesc = formatMessage({
    id: 'api.message.getBankDesc',
    defaultMessage: '查询开户银行列表'
});

const getCarrierDesc = formatMessage({
    id: 'api.message.getCarrierDesc',
    defaultMessage: '查询货代信息'
});

const saleOrderDesc = formatMessage({
    id: 'api.message.saleOrderDesc',
    defaultMessage: '查询整车CBU销售订单'
});

const productCategoryTiledsDesc = formatMessage({
    id: 'api.message.productCategoryTiledsDesc',
    defaultMessage: '查询物料组'
});

const productCategoriesDesc = formatMessage({
    id: 'api.message.productCategoriesDesc',
    defaultMessage: '查询产品信息'
});

const dealerSaleInformationsDesc = formatMessage({
    id: 'api.message.dealerSaleInformationsDesc',
    defaultMessage: '获取总代经营权限'
});

const getMarketDepartmentDesc = formatMessage({
    id: 'api.message.getMarketDepartmentDesc',
    defaultMessage: '查询大区'
});

const getAreasDesc = formatMessage({
    id: 'api.message.getAreasDesc',
    defaultMessage: '查询国家'
});
// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/saleProformaInvoice/init`, null, dto.InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));

// 查询列表
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/saleProformaInvoices/forCurrentUser?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询详情
export const getDetail = id => request(`${baseUrl}/saleProformaInvoices/${id}`, null, dto.Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 新增
export const newOrder = data => request(`${baseUrl}/saleProformaInvoices`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAddDesc));

// 修改
export const modifyOrder = (id, data) => request(`${baseUrl}/saleProformaInvoices/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getModifyDesc));

// 确认
export const confirmData = (id, data) => request(`${baseUrl}/saleProformaInvoices/${id}/confirm`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, confirmDesc));

// 作废
export const abandonData = (id, rowVersion) => request(`${baseUrl}/saleProformaInvoices/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(
        rowVersion
    )
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAbandonDesc));

// 取消pi
export const cancelData = (id, rowVersion) => request(`${baseUrl}/saleProformaInvoices/${id}/cancel`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(
        rowVersion
    )
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getCancelDesc));

// 查询货代信息
export const getCarriers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/carriers/forSaleProformaInvoice?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getCarrierDesc));
};

// 查询开户银行列表
export const getBanks = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/banks/forSaleProformaInvoice?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getBankDesc));
};

// CBU销售订单查询
export const saleOrderQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/saleOrders/forSaleProformaInvoice?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, saleOrderDesc));
};

// CBU销售订单清单查询
export const getSaleOrdersDetails = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/saleOrders/detail/forSaleProformaInvoice?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, saleOrderDesc));
};

// 查询物料组
export const getProductCategoryTileds = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/productCategoryTileds/forSaleProformaInvoice?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, productCategoryTiledsDesc));
};

//查询产品信息
export const getProductCategories = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/productCategories/forSaleProformaInvoice?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, productCategoriesDesc));
};

export const getDealerSaleInformations = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealerSaleInformations/forSaleProformaInvoice?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, dealerSaleInformationsDesc));
};

// 查询大区
export const getMarketDepartment = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketDepartments?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getMarketDepartmentDesc));
};
export const getMarketDepartmentSimple = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketDepartments/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getMarketDepartmentDesc));
};
export const getAreasSimple = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/areas/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getAreasDesc));
};

export const getAreas = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/areas/query?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getAreasDesc));
};
